package easamples;

import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.sparx.Author;
import org.sparx.Client;
import org.sparx.Collection;
import org.sparx.ProjectIssues;
import org.sparx.ProjectResource;
import org.sparx.Repository;
import org.sparx.Task;
import org.sparx.Term;


public class RepositoryExtrasExample implements IExample
{
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Repository Extras";
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#getDescription()
	 */
	public String getDescription()
	{
		return "Examples of how to access repository collections for system level information.<br/><br/><b>Related APIs</b><br/><ul><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?repository3.htm'>Repository API</a></li></ul>";
	}

	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#runExample(java.io.File, javax.swing.DefaultListModel)
	 */
	public void runExample( File projectFile, ExampleUI uiFrame )
	{
		Repository rep = null;
		DefaultListModel outputList = uiFrame.getOutputListModel();
		
		try
		{
			// Create a repository object - This will create a new instance of EA
			rep = new Repository();
			
			// Attempt to open the provided file
			if ( rep.OpenFile(projectFile.getAbsolutePath()) )
			{
				// ==================================================
				// MANAGE PROJECT ISSUES
				// ==================================================
				// Add a project issue
				Collection<ProjectIssues> issues = rep.GetIssues();
				ProjectIssues newIssue = issues.AddNew( "Problem", "Defect" );
				newIssue.Update();
				issues.Refresh();
				
				int newIssueID = newIssue.GetIssueID();
				outputList.addElement( "Added new Issue: " + newIssue.GetName() + " (ID=" + newIssueID + ")" );				
				newIssue = null;
				
				// List all project issues
				for ( short i = 0 ; i < issues.GetCount() ; i++ )
				{
					ProjectIssues currentIssue = issues.GetAt( i );
					String currentIssueName = currentIssue.GetName();
					int currentIssueID = currentIssue.GetIssueID();
					
					outputList.addElement( "Issue: " + currentIssueName );
					
					// Delete the project issue that we just added
					if ( currentIssueID == newIssueID )
					{
						issues.DeleteAt( i, false );
						outputList.addElement( "Deleted issue: " + currentIssueName 
								+ " (ID=" + currentIssueID + ")"  );
					}	
				}
				
				issues = null;
				
				// ==================================================
				// MANAGE TASKS
				// ==================================================
				// Add a task
				Collection<Task> tasks = rep.GetTasks();
				Task newTask = tasks.AddNew( "NewTask", "Defect" );
				newTask.Update();
				tasks.Refresh();
				
				int newTaskID = newTask.GetTaskID();
				outputList.addElement( "Added new Task: " + newTask.GetName() + " (ID=" + newTaskID + ")" );				
				newTask = null;
				
				// List all tasks
				for ( short i = 0 ; i < tasks.GetCount() ; i++ )
				{
					Task currentTask = tasks.GetAt( i );
					String currentTaskName = currentTask.GetName();
					int currentTaskID = currentTask.GetTaskID();
					
					outputList.addElement( "Task: " + currentTaskName );
					
					// Delete the task that we just added
					if ( currentTaskID == newTaskID )
					{
						tasks.DeleteAt( i, false );
						outputList.addElement( "Deleted task: " + currentTaskName 
								+ " (ID=" + currentTaskID + ")"  );
					}
				}				
				
				tasks = null;
				
				// ==================================================
				// MANAGE GLOSSARY TERMS
				// ==================================================
				// Add a terms
				Collection<Term> terms = rep.GetTerms();
				Term newTerm = terms.AddNew( "NewTerm", "Business" );
				newTerm.Update();
				terms.Refresh();
				
				int newTermID = newTerm.GetTermID();
				outputList.addElement( "Added new Term: " + newTerm.GetTerm() + " (ID=" + newTermID + ")" );				
				newTerm = null;
				
				// List all terms
				for ( short i = 0 ; i < terms.GetCount() ; i++ )
				{
					Term currentTerm = terms.GetAt( i );
					String currentTermName = currentTerm.GetTerm();
					int currentTermID = currentTerm.GetTermID();
					
					outputList.addElement( "Term: " + currentTermName );
					
					// Delete the term that we just added
					if ( currentTermID == newTermID )
					{
						terms.DeleteAt( i, false );
						outputList.addElement( "Deleted term: " + currentTermName 
								+ " (ID=" + currentTermID + ")"  );
					}
				}				

				terms = null;
				
				// ==================================================
				// MANAGE AUTHORS
				// ==================================================
				// Add an Author
				Collection<Author> authors = rep.GetAuthors();
				String newAuthorName = "Author P. McAuthor";
				Author newAuthor = authors.AddNew( newAuthorName, "Writer" );
				newAuthor.Update();
				authors.Refresh();
				
				outputList.addElement( "Added new Author: " + newAuthor.GetName() );				
				newAuthor = null;
				
				// List all Authors
				for ( short i = 0 ; i < authors.GetCount() ; i++ )
				{
					Author currentAuthor = authors.GetAt( i );
					String currentAuthorName = currentAuthor.GetName();
					
					outputList.addElement( "Author: " + currentAuthorName );
					
					// Delete the Author that we just added
					if ( currentAuthorName.equals( newAuthorName ) )
					{
						authors.DeleteAt( i, false );
						outputList.addElement( "Deleted author: " + currentAuthorName );
					}
				}				

				authors = null;
				
				// ==================================================
				// MANAGE Clients
				// ==================================================
				// Add a Client
				Collection<Client> clients = rep.GetClients();
				String newClientName = "Joe Client";
				Client newClient = clients.AddNew( newClientName, "Client" );
				newClient.Update();
				clients.Refresh();
				
				outputList.addElement( "Added new Client: " + newClient.GetName() );				
				newClient = null;
				
				// List all Clients
				for ( short i = 0 ; i < clients.GetCount() ; i++ )
				{
					Client currentClient = clients.GetAt( i );
					String currentClientName = currentClient.GetName();
					
					outputList.addElement( "Client: " + currentClientName );
					
					// Delete the Client that we just added
					if ( currentClientName.equals( newClientName ) )
					{
						clients.DeleteAt( i, false );
						outputList.addElement( "Deleted client: " + currentClientName );
					}
				}				

				clients = null;
				
				// ==================================================
				// MANAGE Project Resources
				// ==================================================
				// Add a Project Resource
				Collection<ProjectResource> resources = rep.GetResources();
				String newResourceName = "Senor Resource";
				ProjectResource newResource = resources.AddNew( newResourceName, "Resource" );
				newResource.Update();
				resources.Refresh();
				
				outputList.addElement( "Added new Resource: " + newResource.GetName() );				
				newResource = null;
				
				// List all Project Resources
				for ( short i = 0 ; i < resources.GetCount() ; i++ )
				{
					ProjectResource currentResource = resources.GetAt( i );
					String currentResourceName = currentResource.GetName();
					
					outputList.addElement( "Resource: " + currentResourceName );
					
					// Delete the Project Resource that we just added
					if ( currentResourceName.equals( newResourceName ) )
					{
						resources.DeleteAt( i, false );
						outputList.addElement( "Deleted resource: " + currentResourceName );
					}
				}				

				resources = null;
				
				outputList.addElement( "Done!" );
			}
			else
			{
				// If the file couldn't be opened then notify the user
				String message = "EA was unable to open the file '";
				message += projectFile.getAbsolutePath();
				message += "'.";
				JOptionPane.showMessageDialog( uiFrame, message, "Invalid File", JOptionPane.ERROR_MESSAGE );
				outputList.addElement( message );
			}
		}
		catch ( UnsatisfiedLinkError ule )
		{
			String message = "Unable to communicate with EA.\nPlease ensure that the path to SSJavaCOM.dll included in java.library.path";
			JOptionPane.showMessageDialog( uiFrame, message, "Unable to link to SSJavaCOM.dll", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		catch ( Exception e )
		{
			String message = "An unhandled exception was thrown: " + e.getMessage();
			JOptionPane.showMessageDialog( uiFrame, message, "Unhandled Exception", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		finally
		{
			if ( rep != null )
			{
				// Clean up
				rep.CloseFile();
				rep.Exit();
				rep.destroy();
			}
		}
	}

}
